package com.project.auth.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.auth.mapper.WechatMapper;
import com.project.auth.model.wechat.Menu;
import com.project.auth.service.WechatService;
import com.project.auth.vo.wechat.MenuVo;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author XCM
 * @Date 2023/06/07 19:48
 */
@Service
public class WechatServiceImpl extends ServiceImpl<WechatMapper, Menu> implements WechatService {
    @Autowired
    private WxMpService wxMpService;

    /**
     * 获取全部菜单
     *
     * @return
     */
    @Override
    public List<MenuVo> findMenuInfo() {
        List<MenuVo> menuVoList = new ArrayList<>();
        //1.查询全部菜单
        List<Menu> menus = baseMapper.selectList(null);
        //2.查询所有一级菜单
        List<Menu> firstMenuList = menus.stream().filter(menu -> menu.getParentId() == 0).collect(Collectors.toList());
        //3.遍历一级菜单
        for (Menu firstMenu : firstMenuList) {
            MenuVo firstMenuVo = new MenuVo();
            BeanUtils.copyProperties(firstMenu, firstMenuVo);
            //4.将id和parentId对比,获得一级菜单下的二级菜单
            List<Menu> secondMenuList = menus.stream().filter(menu -> menu.getParentId() == firstMenu.getId()).collect(Collectors.toList());
            //5.将二级菜单封装到一级菜单的children集合中
            List<MenuVo> children = new ArrayList<>();
            for (Menu secondMenu : secondMenuList) {
                MenuVo secondMenuVo = new MenuVo();
                BeanUtils.copyProperties(secondMenu, secondMenuVo);
                children.add(secondMenuVo);
            }
            firstMenuVo.setChildren(children);
            //放到最终返回的list集合中
            menuVoList.add(firstMenuVo);
        }
        return menuVoList;
    }

    /**
     * 同步菜单
     */
    @Override
    public void syncMenu() {
        //查询菜单数据,封装为微信要求格式
        List<MenuVo> menuVoList = this.findMenuInfo();
        //菜单
        JSONArray buttonList = new JSONArray();
        for (MenuVo FirstMenuVo : menuVoList) {
            JSONObject firstMenuJson = new JSONObject();
            firstMenuJson.put("name", FirstMenuVo.getName());
            if (CollectionUtils.isEmpty(FirstMenuVo.getChildren())) {
                firstMenuJson.put("type", FirstMenuVo.getType());
                firstMenuJson.put("url", "http://xiypgt.natappfree.cc/#" + FirstMenuVo.getUrl());
            } else {
                JSONArray subButton = new JSONArray();
                for (MenuVo secondMenuVo : FirstMenuVo.getChildren()) {
                    JSONObject view = new JSONObject();
                    view.put("type", secondMenuVo.getType());
                    if (secondMenuVo.getType().equals("view")) {
                        view.put("name", secondMenuVo.getName());
                        //H5页面地址
                        view.put("url", "http://xiypgt.natappfree.cc#" + secondMenuVo.getUrl());
                    } else {
                        view.put("name", secondMenuVo.getName());
                        view.put("key", secondMenuVo.getMeunKey());
                    }
                    subButton.add(view);
                }
                firstMenuJson.put("sub_button", subButton);
            }
            buttonList.add(firstMenuJson);
        }
        //菜单
        JSONObject button = new JSONObject();
        button.put("button", buttonList);
        try {
            //调用工具中的方法实现菜单推送
            wxMpService.getMenuService().menuCreate(button.toJSONString());
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 删除菜单
     */
    @Override
    public void removeMenu() {
        try {
            wxMpService.getMenuService().menuDelete();
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
    }
}
